package java复习202408.D_比较方法验证;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;

public class d1_comparatorAndComparable implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        return 0;
    }

    public class D1 implements Comparable<D1> {
        String name;
        byte age ;

        public D1(String name, byte age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public byte getAge() {
            return age;
        }

        public void setAge(Byte age) {
            this.age = age;
        }

        @Override
        public int compareTo(D1 targeAge) {
            return this.age > targeAge.getAge() ? 1 : this.age == targeAge.getAge() ? 0 : -1;
        }

        @Override
        public String toString() {
            return "D1{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    @Test
    public void comparableTest(){
        D1 tom = new D1("TOM", (byte) 13);
        D1 jerry = new D1("JERRY", (byte) 11);
        System.out.println(tom.compareTo(jerry));

        ArrayList<D1> d1s = new ArrayList<>();
        d1s.add(tom);
        d1s.add(jerry);

        d1s.sort(new Comparator<D1>() {
            @Override
            public int compare(D1 o1, D1 o2) {
                return o1.age > o2.getAge() ? 1 : o1.age == o2.getAge() ? 0 : -1;
            }
        });

        System.out.println(d1s);

    }





}
